package com.atxiaodei.springbootkabudazkapi.zookeepercrud;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @ClassName DeleteZookeeperDemo
 * @Deacription TODO
 * @Author 卡布达
 * @Date 2022/1/16 22:08
 * @Version 1.0
 **/
public class DeleteZookeeperDemo {

    public void delete() throws Exception {

        CuratorFramework client= CuratorFrameworkFactory
                .builder()
                .connectionTimeoutMs(50000)
                .connectString("192.168.0.108:2181") //读写分离(zookeeper-server)
                // baseSleepTimeMs*Math.max(1,random.nextInt(1<<(maxRetries+1))
                /**
                 * RetryNTimes 指定最大重试次数
                 * RetryOneTimes
                 * RetryUntilElapsed 一直重试，直到达到规定时间
                 */
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .sessionTimeoutMs(15000)
                // 名称空间 每次操作zk /kabuda
                .namespace("kabuda")
                .build();
        client.start(); //启动
        // 普通删除
        client.delete().forPath("/app1");
        // 删除带子节点的节点
        client.delete().deletingChildrenIfNeeded().forPath("/app4");
        // 保证一定删除节点
        client.delete().guaranteed().forPath("/app2");
        // 回调方式删除
        client.delete().inBackground((x, event) -> System.out.println(event)).forPath("/app3");
    }

    public static void main(String[] args) throws Exception {
        DeleteZookeeperDemo deleteZookeeperDemo = new DeleteZookeeperDemo();
        deleteZookeeperDemo.delete();

    }
}
